import VueCookies from 'vue-cookies'

let loginUserKey="loginUserKey";
let tokenExpireTimes = 60 * 19;
VueCookies.config();
///es6 语法 定义一个datetools对象,  在它下面定义相应的方法
let cookieTools = {
  //获取cookie
  get: function (key) {
    return VueCookies.get(key);
  },
  //expireTimes 过期时间 可以是字符串,比如"1d"代表1天 ,也可以是数字,以秒为单位,比如 60 为60秒
  set: function (key, value, expireTimes) {
    VueCookies.set(key, value, expireTimes);
  },
  setLoginUser:function(lu)
  {
    var loguinUser={
      user_name: lu.user_name,
      user_full_name: lu.user_full_name,
      token: lu.token,
    }
    var loginUserJson= JSON.stringify(loguinUser);

    VueCookies.set(loginUserKey,loginUserJson,tokenExpireTimes);
  },
  getLoginUser:function()
  {
    var loginUser=VueCookies.get(loginUserKey);   //提取的时候VueCookies会自动把json字符串转换为对象
    if(loginUser)return loginUser;
    return null;
  },
  removeLoginUser:function()   //删除登录用户cookie
  {
    VueCookies.remove(loginUserKey)    ;
  },  
  getToken: function () {
    var loginUser= this.getLoginUser();

    if(loginUser)return loginUser.token;
    return null;

  }

}
export default cookieTools
